#include "CShopState.h"

#include "Game Objects\CPlayer.h"
#include "CGame.h"
#include "Wrappers/CSGD_DirectInput.h"
#include "CPrintBitmapToScreen.h"

CShopState::CShopState(void)
{
	m_nCurrentSelection = 0;
	m_nCursorID = -1;
	m_nBackgroundImageID = -1;
	m_nBackgroundMusicID = -1;
}

CShopState::CShopState(const CShopState&)
{

}

CShopState& CShopState::operator=(const CShopState&)
{
	return *this;
}

CShopState::~CShopState(void)
{

}

CShopState* CShopState::GetInstance(void)
{
	static CShopState instance;
	return &instance;
}

void CShopState::Enter(void)
{
	// Load resources
	m_nCursorID = CSGD_TextureManager::GetInstance()->LoadTexture("resource/Graphics/SGP_Cursor.png");
	m_nMovementSFXID = CSGD_FModManager::GetInstance()->LoadSound("resource/Sound/SGP_MenuSelection.wav");
	m_nBackgroundImageID = CSGD_TextureManager::GetInstance()->LoadTexture("resource/Graphics/ShopBG.bmp");

	// Play intro music

}

bool CShopState::Input(void)
{
	if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_F1) || CSGD_DirectInput::GetInstance()->JoystickButtonPressed(5))
	{
		CPlayer::GetInstance()->SetMoney(CPlayer::GetInstance()->GetMoney() + 1000);
	}
	if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_UP) || CSGD_DirectInput::GetInstance()->JoystickGetLStickDirPressed(DIR_UP))
	{
		if(m_nCurrentSelection != 0)
		{
			--m_nCurrentSelection;
		}
		else
			m_nCurrentSelection = 6;

		CSGD_FModManager::GetInstance()->PlaySoundA(m_nMovementSFXID);
	}

	if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_DOWN) || CSGD_DirectInput::GetInstance()->JoystickGetLStickDirPressed(DIR_DOWN))
	{
		if(m_nCurrentSelection == 6)
		{
			m_nCurrentSelection = 0;
		}
		else if(m_nCurrentSelection == 12)
			m_nCurrentSelection = 6;
		else
			++m_nCurrentSelection;

		CSGD_FModManager::GetInstance()->PlaySoundA(m_nMovementSFXID);
	}

	if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_LEFT) || CSGD_DirectInput::GetInstance()->JoystickGetLStickDirPressed(DIR_LEFT))
	{
		if(m_nCurrentSelection >= 7 && m_nCurrentSelection <= 12)
			m_nCurrentSelection -= 7;
		
		CSGD_FModManager::GetInstance()->PlaySoundA(m_nMovementSFXID);
	}

	if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_RIGHT)|| CSGD_DirectInput::GetInstance()->JoystickGetLStickDirPressed(DIR_RIGHT))
	{
		if(m_nCurrentSelection >= 0 && m_nCurrentSelection <= 5)
			m_nCurrentSelection += 7;

		CSGD_FModManager::GetInstance()->PlaySoundA(m_nMovementSFXID);
	}

	if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_RETURN)|| CSGD_DirectInput::GetInstance()->JoystickButtonPressed(0))
	{
		CPlayer* player = CPlayer::GetInstance();

		if(m_nCurrentSelection == 0 && player->GetShotgunClip() == 0 && player->GetMoney() >= 300) 
		{// Shotgun
			player->SetShotgunClip(1);
			player->SetMoney(player->GetMoney() - 300);
		}
		else if(m_nCurrentSelection == 1 && player->GetMoney() >= 50) 
		{// Pistol Ammo
			int prevAmmo = player->GetPistolAmmo();
			player->SetPistolAmmo(player->GetPistolAmmo() + 20);
			if(prevAmmo < player->GetPistolAmmo())
				player->SetMoney(player->GetMoney() - 50);
		}
		else if(m_nCurrentSelection == 2 && player->GetMoney() >= 100) 
		{// Shotgun Ammo
			int prevAmmo = player->GetShotgunAmmo();
			player->SetShotgunAmmo(player->GetShotgunAmmo() + 5);
			if(prevAmmo < player->GetShotgunAmmo())
				player->SetMoney(player->GetMoney() - 100);
		}
		else if(m_nCurrentSelection == 3 && player->GetMoney() >= 300) 
		{// Rocket Ammo
			int prevAmmo = player->GetRocketAmmo();
			player->SetRocketAmmo(player->GetRocketAmmo() + 2);
			if(prevAmmo < player->GetRocketAmmo())
				player->SetMoney(player->GetMoney() - 300);
		}
		else if(m_nCurrentSelection == 4 && player->GetMoney() >= 300) 
		{// FW Grenades
			int prevAmmo = player->GetFwGrenades();
			player->SetFwGrenades(player->GetFwGrenades() + 3);
			if(prevAmmo < player->GetFwGrenades())
				player->SetMoney(player->GetMoney() - 300);
		}
		else if(m_nCurrentSelection == 5 && player->GetMoney() >= 300) 
		{// Ex Grenades
			int prevAmmo = player->GetExGrenades();
			player->SetExGrenades(player->GetExGrenades() + 3);
			if(prevAmmo < player->GetExGrenades())
				player->SetMoney(player->GetMoney() - 300);
		}
		else if(m_nCurrentSelection == 6) 
		{// Exit
			CGame::GetInstance()->PopState();
		}
		else if(m_nCurrentSelection == 7 && player->GetRocketClip() == 0 && player->GetMoney() >= 1000) 
		{// Rocket
			player->SetRocketClip(1);
			player->SetMoney(player->GetMoney() - 1000);
		}
		else if(m_nCurrentSelection == 8 && player->GetMoney() >= 100) 
		{// Pistol Clip
			player->SetPistolClip(player->GetPistolClip() + 1);
			player->SetMoney(player->GetMoney() - 100);
		}
		else if(m_nCurrentSelection == 9 && player->GetMoney() >= 300 && player->GetShotgunClip() != 0)
		{// Shotgun Clip
			player->SetShotgunClip(player->GetShotgunClip() + 1);
			player->SetMoney(player->GetMoney() - 300);
		}
		else if(m_nCurrentSelection == 10 && player->GetMoney() >= 600 && player->GetRocketClip() != 0) 
		{// Rocket Clip
			player->SetRocketClip(player->GetRocketClip() + 1);
			player->SetMoney(player->GetMoney() - 600);
		}
		else if(m_nCurrentSelection == 11 && player->GetMoney() >= 600 && player->GetFWGrenadeClip() != 0) 
		{// FW Clip
			player->SetFWGrenadeClip(player->GetFWGrenadeClip() + 1);
			player->SetMoney(player->GetMoney() - 600);
		}
		else if(m_nCurrentSelection == 12 && player->GetMoney() >= 600 && player->GetExGrenadeClip() != 0) 
		{// Ex Clip
			player->SetExGrenadeClip(player->GetExGrenadeClip() + 1);
			player->SetMoney(player->GetMoney() - 600);
		}
	}

	return true;
}

void CShopState::Update(float fElapsedTime)
{

}

void CShopState::Render(void)
{
	CPlayer* player = CPlayer::GetInstance();

	RECT rBackground;
	rBackground.left = 0;
	rBackground.top = 0;
	rBackground.right = rBackground.left + CGame::GetInstance()->GetScreenWidth();
	rBackground.bottom = rBackground.top + CGame::GetInstance()->GetScreenHeight();

	// Draw the background image
	CSGD_TextureManager::GetInstance()->Draw(m_nBackgroundImageID, 0, 0, 1.0f, 1.0f, &rBackground);

	const int leftCol = 120;
	const int rightCol = 360;

	int rowPos[7] = {110, 170, 210, 250, 290, 330, 360};
	char buffer[128];
	sprintf_s(buffer, _countof(buffer), "%i", player->GetMoney());
	CPrintBitmapFontToScreen::GetInstance()->Print("Money: ", leftCol + 32, rowPos[6], 1.0f, D3DCOLOR_XRGB(0,255,0));
	CPrintBitmapFontToScreen::GetInstance()->Print(buffer, leftCol + 128, rowPos[6], 1.0f, D3DCOLOR_XRGB(255,255,0));
	// Display menu options
	if(m_nCurrentSelection == 0)
		CPrintBitmapFontToScreen::GetInstance()->Print("Shotgun (300)", leftCol, rowPos[0], 1.0f, D3DCOLOR_XRGB(255,0,0));
	else
		CPrintBitmapFontToScreen::GetInstance()->Print("Shotgun (300)", leftCol, rowPos[0], 1.0f, D3DCOLOR_XRGB(0,255,0));

	
	sprintf_s(buffer, _countof(buffer), "%i", player->GetPistolAmmo());
	CPrintBitmapFontToScreen::GetInstance()->Print(buffer, 200, (rowPos[0] + rowPos[1] + 20)>>1, 0.7f, D3DCOLOR_XRGB(255,255,0));
	CPrintBitmapFontToScreen::GetInstance()->Print("Pistol", 280, (rowPos[0] + rowPos[1] + 20)>>1, 0.7f, D3DCOLOR_XRGB(255,255,0));

	if(m_nCurrentSelection == 1)
		CPrintBitmapFontToScreen::GetInstance()->Print("Ammo (50)", leftCol, rowPos[1], 0.6f, D3DCOLOR_XRGB(255,0,0));
	else
		CPrintBitmapFontToScreen::GetInstance()->Print("Ammo (50)", leftCol, rowPos[1], 0.6f, D3DCOLOR_XRGB(0,255,0));

	sprintf_s(buffer, _countof(buffer), "%i", player->GetShotgunAmmo());
	if(player->GetShotgunClip() != 0)
	{
		CPrintBitmapFontToScreen::GetInstance()->Print(buffer, 200, (rowPos[1] + rowPos[2])>>1, 0.7f, D3DCOLOR_XRGB(255,255,0));
		CPrintBitmapFontToScreen::GetInstance()->Print("Shotgun", 280, (rowPos[1] + rowPos[2])>>1, 0.7f, D3DCOLOR_XRGB(255,255,0));
	}
	else
	{
		CPrintBitmapFontToScreen::GetInstance()->Print(buffer, 200, (rowPos[1] + rowPos[2])>>1, 0.7f, D3DCOLOR_XRGB(100,100,100));
		CPrintBitmapFontToScreen::GetInstance()->Print("Shotgun", 280, (rowPos[1] + rowPos[2])>>1, 0.7f, D3DCOLOR_XRGB(100,100,100));
	}

	if(m_nCurrentSelection == 2)
		CPrintBitmapFontToScreen::GetInstance()->Print("Ammo (100)", leftCol, rowPos[2], 0.6f, D3DCOLOR_XRGB(255,0,0));
	else																		
		CPrintBitmapFontToScreen::GetInstance()->Print("Ammo (100)", leftCol, rowPos[2], 0.6f, D3DCOLOR_XRGB(0,255,0));

	sprintf_s(buffer, _countof(buffer), "%i", player->GetRocketAmmo());
	if(player->GetRocketClip() != 0)
	{
		CPrintBitmapFontToScreen::GetInstance()->Print(buffer, 200, (rowPos[2] + rowPos[3])>>1, 0.7f, D3DCOLOR_XRGB(255,255,0));
		CPrintBitmapFontToScreen::GetInstance()->Print("Rocket", 280, (rowPos[2] + rowPos[3])>>1, 0.7f, D3DCOLOR_XRGB(255,255,0));
	}
	else
	{
		CPrintBitmapFontToScreen::GetInstance()->Print(buffer, 200, (rowPos[2] + rowPos[3])>>1, 0.7f, D3DCOLOR_XRGB(100,100,100));
		CPrintBitmapFontToScreen::GetInstance()->Print("Rocket", 280, (rowPos[2] + rowPos[3])>>1, 0.7f, D3DCOLOR_XRGB(100,100,100));
	}

	if(m_nCurrentSelection == 3)
		CPrintBitmapFontToScreen::GetInstance()->Print("Ammo (300)", leftCol, rowPos[3], 0.6f, D3DCOLOR_XRGB(255, 0, 0));
	else												
		CPrintBitmapFontToScreen::GetInstance()->Print("Ammo (300)", leftCol, rowPos[3], 0.6f, D3DCOLOR_XRGB(0, 255, 0));

	sprintf_s(buffer, _countof(buffer), "%i", player->GetFwGrenades());
	CPrintBitmapFontToScreen::GetInstance()->Print(buffer, 200, (rowPos[3] + rowPos[4])>>1, 0.7f, D3DCOLOR_XRGB(255,255,0));
	CPrintBitmapFontToScreen::GetInstance()->Print("FW Grenade", 280, (rowPos[3] + rowPos[4])>>1, 0.7f, D3DCOLOR_XRGB(255,255,0));

	if(m_nCurrentSelection == 4)
		CPrintBitmapFontToScreen::GetInstance()->Print("Ammo (300)", leftCol, rowPos[4], 0.6f, D3DCOLOR_XRGB(255, 0, 0));
	else												
		CPrintBitmapFontToScreen::GetInstance()->Print("Ammo (300)", leftCol, rowPos[4], 0.6f, D3DCOLOR_XRGB(0, 255, 0));

	sprintf_s(buffer, _countof(buffer), "%i", player->GetExGrenades());
	CPrintBitmapFontToScreen::GetInstance()->Print(buffer, 200, (rowPos[4] + rowPos[5])>>1, 0.7f, D3DCOLOR_XRGB(255,255,0));
	CPrintBitmapFontToScreen::GetInstance()->Print("Ex Grenade", 280, (rowPos[4] + rowPos[5])>>1, 0.7f, D3DCOLOR_XRGB(255,255,0));

	if(m_nCurrentSelection == 5)
		CPrintBitmapFontToScreen::GetInstance()->Print("Ammo (300)", leftCol, rowPos[5], 0.6f, D3DCOLOR_XRGB(255, 0, 0));
	else												
		CPrintBitmapFontToScreen::GetInstance()->Print("Ammo (300)", leftCol, rowPos[5], 0.6f, D3DCOLOR_XRGB(0, 255, 0));

	if(m_nCurrentSelection == 6)
		CPrintBitmapFontToScreen::GetInstance()->Print("EXIT", rightCol + 30, rowPos[6], 1.0f, D3DCOLOR_XRGB(255,0,0));
	else
		CPrintBitmapFontToScreen::GetInstance()->Print("EXIT", rightCol + 30, rowPos[6], 1.0f, D3DCOLOR_XRGB(0,255,0));

	if(m_nCurrentSelection == 7)
		CPrintBitmapFontToScreen::GetInstance()->Print("Rocket (1000)", rightCol - 30, rowPos[0], 1.0f, D3DCOLOR_XRGB(255,0,0));
	else
		CPrintBitmapFontToScreen::GetInstance()->Print("Rocket (1000)", rightCol - 30, rowPos[0], 1.0f, D3DCOLOR_XRGB(0,255,0));

	if(m_nCurrentSelection == 8)
		CPrintBitmapFontToScreen::GetInstance()->Print("Clip Size (100)", rightCol, rowPos[1], 0.6f, D3DCOLOR_XRGB(255,0,0));
	else													   
		CPrintBitmapFontToScreen::GetInstance()->Print("Clip Size (100)", rightCol, rowPos[1], 0.6f, D3DCOLOR_XRGB(0,255,0));
															   
	if(m_nCurrentSelection == 9)							   
		CPrintBitmapFontToScreen::GetInstance()->Print("Clip Size (300)", rightCol, rowPos[2], 0.6f, D3DCOLOR_XRGB(255,0,0));
	else													   
		CPrintBitmapFontToScreen::GetInstance()->Print("Clip Size (300)", rightCol, rowPos[2], 0.6f, D3DCOLOR_XRGB(0,255,0));
															   
	if(m_nCurrentSelection == 10)							   
		CPrintBitmapFontToScreen::GetInstance()->Print("Clip Size (600)", rightCol, rowPos[3], 0.6f, D3DCOLOR_XRGB(255,0,0));
	else													   
		CPrintBitmapFontToScreen::GetInstance()->Print("Clip Size (600)", rightCol, rowPos[3], 0.6f, D3DCOLOR_XRGB(0,255,0));

	if(m_nCurrentSelection == 11)							   
		CPrintBitmapFontToScreen::GetInstance()->Print("Clip Size (600)", rightCol, rowPos[4], 0.6f, D3DCOLOR_XRGB(255,0,0));
	else													   
		CPrintBitmapFontToScreen::GetInstance()->Print("Clip Size (600)", rightCol, rowPos[4], 0.6f, D3DCOLOR_XRGB(0,255,0));

	if(m_nCurrentSelection == 12)							   
		CPrintBitmapFontToScreen::GetInstance()->Print("Clip Size (600)", rightCol, rowPos[5], 0.6f, D3DCOLOR_XRGB(255,0,0));
	else													   
		CPrintBitmapFontToScreen::GetInstance()->Print("Clip Size (600)", rightCol, rowPos[5], 0.6f, D3DCOLOR_XRGB(0,255,0));

	// Draw the cursor
	/*if(m_nCurrentSelection <= 6)
		CSGD_TextureManager::GetInstance()->Draw(m_nCursorID, 140, 250 + m_nCurrentSelection*30);
	else
		CSGD_TextureManager::GetInstance()->Draw(m_nCursorID, 350, 250);*/
}

void CShopState::Exit(void)
{
	// Unload assets
	CSGD_TextureManager::GetInstance()->UnloadTexture(m_nCursorID);
	CSGD_FModManager::GetInstance()->UnloadSound(m_nMovementSFXID);
	CSGD_TextureManager::GetInstance()->UnloadTexture(m_nBackgroundImageID);
}